<?php

class RequestHandler{

	private $controller;
	private $action;
	private $request;

	public function __construct(){
		if(!isset($_GET['request']) || $_GET['request'] == ''){
			$this->request = "";
			$this->initialize_index();
		} else {
			$this->request = $_GET['request'];
			$this->initialize();
		}
	}


	private function initialize_index(){
		$this->controller_path = "controller/public/IndexController.php";
		$this->controller_name = "IndexController";
		$this->action = "initialize";
	}


	private function initialize(){
		$request_parts = explode(".", $this->request);
		if(count($request_parts) < 2){
			throw new Exception("Bad Request path: ". $this->request);
		}
		
		// get controller path
		$controller_path = "controller/";
		for($i = 0; $i < count($request_parts) - 2; $i++){
			$controller_path .= $request_parts[$i]."/"; 
		}
		
		// get controller name
		$controller_name = $request_parts[count($request_parts) - 2];
		$controller_name = $this->capitalize($controller_name);
		$this->controller_name = $controller_name . "Controller";
		$this->controller_path .= $controller_path . $this->controller_name . ".php";
		$this->action = $request_parts[count($request_parts)-1];
	}


	private function capitalize($str){
		$res = "";
		$str_parts = explode("_", $str);
		foreach($str_parts as $index => $str_part){
			$res .= ucwords($str_part);
		}
		
		return $res;
	}

	public function get_controller_name(){
		return $this->controller_name;
	}

	public function get_controller_path(){
		return $this->controller_path;
	}

	public function get_action(){
		return $this->action;
	}

	public function get_request(){
		return $this->request;
	}
}

?>
